from PyQt5.QtWidgets import QMessageBox


def isdigit(ss):
    """检查字符串是否为数字（支持小数）"""
    if ss.isdigit():
        return True
    elif "." in ss:
        s = ss.split(".")
        if len(s) > 2:
            return False
        else:
            for si in s:
                if not si.isdigit():
                    return False
            return True
    else:
        return False


def strRound2(num):
    if type(num) == int:
        return str(num)
    return "{:.2f}".format(float(num))


def showMessageBox(text):
    # 创建一个信息框
    msg = QMessageBox()
    msg.setWindowTitle('提示')
    msg.setText(text)
    msg.setIcon(QMessageBox.Information)
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

    result = msg.exec_()  # 显示对话框并等待用户操作
    if result == QMessageBox.Ok:
        return True
    else:
        return False




# excel读取写入
#
#     # 先默认数据格式确定
#     def readExcel(self, name=None):
#         if name is None or name == "":
#             showMessageBox("请输入客户名")
#             return
#         filePath = self.FileDict.get(name)
#         if filePath is None:
#             result = showMessageBox("还没有为{}建立账本，是否新建？".format(name))
#             if result:
#                 fromFile = os.path.join(os.path.abspath("."), "模板.xlsx")
#                 copyFile = os.path.join(os.path.abspath("."), name + ".xlsx")
#                 try:
#                     shutil.copy(fromFile, copyFile)
#                     filePath = copyFile
#                 except IOError as e:
#                     showMessageBox("Unable to copy file. %s" % e)
#                     return
#
#         try:
#             app = xw.App(visible=False, add_book=False)
#             wb = app.books.open(filePath)
#             self.xwApp = app
#             self.openFile = wb
#         except Exception as e:
#             showMessageBox("读取文件失败:" + str(e))
#
#     @property
#     def openSheet(self):
#         return self.openFile.sheets[str(self.date[1]) + '月']
#
#     @property
#     def usedRange(self):
#         # range = sht.used_range
#         try:
#             rowNum = self.openSheet.used_range.rows.count
#             # 表 格式： A日期 B名称及规格 C单位 D数量 E单价 F备注 G金额
#             curRange = self.openSheet.range('A1', 'G' + str(rowNum))
#             return curRange
#         except Exception as e:
#             showMessageBox("获取数据错误:" + str(e))
#
#     def save(self):
#         items = UIManagerInstance().getItems()
#         check = self.checkDataValid(items)
#         if check != "":
#             showMessageBox(check)
#             return False
#
#         self.readExcel(UIManagerInstance().getCustomer())
#         if self.openFile is None:
#             showMessageBox("无法打开文件")
#             return False
#
#         result = []
#         # 月.日
#         date = str(self.date[1]) + "." + str(self.date[2])
#         for item in items:
#             rowData = [date]
#             for key in ITEM_INDEX:
#                 rowData.append(item[key])
#             result.append(rowData)
#         # print(result)
#         row = self.usedRange.rows.count
#         try:
#             self.openSheet.range('A' + str(row + 1)).value = result
#             self.openFile.save()
#             return True
#         except Exception as e:
#             showMessageBox("保存问题:" + str(e))
